package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.domain.ResponseResult;
import org.example.domain.dto.GalleryListDto;
import org.example.domain.entity.Gallery;
import org.example.domain.vo.PageVo;
import org.example.mapper.GalleryMapper;
import org.example.service.GalleryService;
import org.example.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class GalleryServiceImpl extends ServiceImpl<GalleryMapper, Gallery> implements GalleryService {

    private final GalleryMapper galleryMapper;

    @Override
    public ResponseResult addGallery(Gallery gallery) {

        Integer id = SecurityUtils.getUserId();
        gallery.setCreateBy(id);
        gallery.setUpdateBy(id);
        return ResponseResult.okResult(save(gallery));
    }

    @Override
    public ResponseResult galleryList(GalleryListDto galleryListDto) {

        LambdaQueryWrapper<Gallery> queryWrapper = new LambdaQueryWrapper<>();
        if(galleryListDto.getStatus() != null) {
            queryWrapper.eq(Gallery::getStatus,galleryListDto.getStatus());
        }
        Page<Gallery> page = new Page<>();
        page.setSize(galleryListDto.getPageSize());
        page.setCurrent(galleryListDto.getPageNum());
        page(page,queryWrapper);
        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult updateGallery(Gallery gallery) {
        Integer id = SecurityUtils.getUserId();
        gallery.setUpdateBy(id);
        return ResponseResult.okResult(updateById(gallery));
    }

    @Override
    public ResponseResult recommendGallery(GalleryListDto galleryListDto) {
        LambdaQueryWrapper<Gallery> queryWrapper = new LambdaQueryWrapper<>();
        if(galleryListDto.getStatus() != null) {
            queryWrapper.eq(Gallery::getStatus,galleryListDto.getStatus());
        }
        Page<Gallery> page = new Page<>();
        page.setSize(galleryListDto.getPageSize());
        page.setCurrent(galleryListDto.getPageNum());
        page(page,queryWrapper);
        return ResponseResult.okResult(page.getRecords());
    }

    @Override
    public ResponseResult allGalleryList() {
        List<Gallery> galleries = galleryMapper.allGalleryList();
        return ResponseResult.okResult(galleries);
    }

}
